#!/bin/bash

# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# Base URL
BASE_URL="http://localhost:8000"

# Function to print test result
print_result() {
    if [ $1 -eq 0 ]; then
        echo -e "${GREEN}✓ $2 passed${NC}"
    else
        echo -e "${RED}✗ $2 failed${NC}"
    fi
}

echo "Starting API Tests..."
echo "===================="

# Test 1: List available tools
echo -e "\nTesting GET /agent/tools"
response=$(curl -s -w "%{http_code}" $BASE_URL/agent/tools)
http_code=${response: -3}
content=${response:0:${#response}-3}
print_result $? "List tools"
echo "Response: $content"

# Test 2: Create a new task
echo -e "\nTesting POST /agent/task"
response=$(curl -s -w "%{http_code}" -X POST \
    -H "Content-Type: application/json" \
    -d '{"description": "Generate a presentation about AI", "parameters": {"topic": "AI"}}' \
    $BASE_URL/agent/task)
http_code=${response: -3}
content=${response:0:${#response}-3}
task_id=$(echo $content | grep -o '"task_id":"[^"]*' | cut -d'"' -f4)
print_result $? "Create task"
echo "Response: $content"

# Test 3: Get task status
echo -e "\nTesting GET /agent/task/{task_id}"
response=$(curl -s -w "%{http_code}" $BASE_URL/agent/task/$task_id)
http_code=${response: -3}
content=${response:0:${#response}-3}
print_result $? "Get task status"
echo "Response: $content"

# Test 4: Process task
echo -e "\nTesting POST /agent/task/{task_id}/process"
response=$(curl -s -w "%{http_code}" -X POST $BASE_URL/agent/task/$task_id/process)
http_code=${response: -3}
content=${response:0:${#response}-3}
print_result $? "Process task"
echo "Response: $content"

# Test 5: Generate PPT
echo -e "\nTesting POST /agent/generate-ppt"
response=$(curl -s -w "%{http_code}" -X POST \
    -H "Content-Type: application/json" \
    -d '{"topic": "Artificial Intelligence Trends 2024"}' \
    $BASE_URL/agent/generate-ppt)
http_code=${response: -3}
content=${response:0:${#response}-3}
filename=$(echo $content | grep -o '"file_path":"[^"]*' | cut -d'"' -f4 | awk -F'/' '{print $NF}')
print_result $? "Generate PPT"
echo "Response: $content"

# Test 6: Download PPT (if previous test succeeded)
if [ ! -z "$filename" ]; then
    echo -e "\nTesting GET /agent/download-ppt/{filename}"
    response=$(curl -s -w "%{http_code}" -O -J $BASE_URL/agent/download-ppt/$filename)
    http_code=${response: -3}
    print_result $? "Download PPT"
fi

# Test 7: Test error handling - Invalid task ID
echo -e "\nTesting error handling - Invalid task ID"
response=$(curl -s -w "%{http_code}" $BASE_URL/agent/task/invalid-id)
http_code=${response: -3}
content=${response:0:${#response}-3}
print_result "$([ $http_code -eq 404 ])" "Error handling - Invalid task ID"
echo "Response: $content"

# Test 8: Test error handling - Invalid JSON
echo -e "\nTesting error handling - Invalid JSON"
response=$(curl -s -w "%{http_code}" -X POST \
    -H "Content-Type: application/json" \
    -d '{"invalid json"}' \
    $BASE_URL/agent/task)
http_code=${response: -3}
content=${response:0:${#response}-3}
print_result "$([ $http_code -eq 422 ])" "Error handling - Invalid JSON"
echo "Response: $content"

# Test 9: Test PPT generation with custom parameters
echo -e "\nTesting POST /agent/generate-ppt with custom parameters"
response=$(curl -s -w "%{http_code}" -X POST \
    -H "Content-Type: application/json" \
    -d '{
        "topic": "Modern Software Architecture",
        "output_filename": "architecture.pptx",
        "parameters": {
            "num_slides": 10,
            "include_outline": true,
            "style": "professional"
        }
    }' \
    $BASE_URL/agent/generate-ppt)
http_code=${response: -3}
content=${response:0:${#response}-3}
print_result $? "Generate PPT with custom parameters"
echo "Response: $content"

# Test 10: Test concurrent task creation
echo -e "\nTesting concurrent task creation"
for i in {1..3}; do
    response=$(curl -s -w "%{http_code}" -X POST \
        -H "Content-Type: application/json" \
        -d "{\"description\": \"Concurrent task $i\", \"parameters\": {\"priority\": $i}}" \
        $BASE_URL/agent/task) &
done
wait
print_result $? "Concurrent task creation"

echo -e "\nAPI Tests Completed"
echo "====================" 